package com.soft.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import com.soft.admin.common.ElementConst;
import com.soft.dao.ChannelDao;
import com.soft.dao.DeviceDao;
import com.soft.front.dto.FDeviceDTO;
import com.soft.front.dto.LocationDTO;
import com.soft.model.Channel;
import com.soft.model.Device;
import com.soft.support.Pager;

@SuppressWarnings("unchecked")
@Component
public class DeviceDaoImpl extends BaseDaoImpl implements DeviceDao {
	
	private ChannelDao channelDao;
	
	@Autowired
	public void setChannelDao(ChannelDao channelDao) {
		this.channelDao = channelDao;
	}

//	@Cacheable(value = "DEVICE_INFO_CACHE", key = "#id")
	public Device get(Integer id) {
		return (Device)this.get(Device.class, id);
	}
	
	@CacheEvict(value = "DEVICE_INFO_CACHE", allEntries = true)
	public void addOrEdit(Device device) {
		this.saveOrUpdate(device);
	}
	
	@CacheEvict(value = "DEVICE_INFO_CACHE", allEntries = true)
	public void remove(Device device) {
		this.delete(device);
	}

	@Cacheable(value = "DEVICE_INFO_CACHE", key = "#index + #page + #pageSize + 'getLst'")
	public Map<String, Object> getLst(String index, Integer page, Integer pageSize) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		//分页
		if (page == null) page = 1;
		Integer startIndex = (page - 1) * ElementConst.PAGENUM;
//		Integer endIndex = startIndex + ElementConst.PAGENUM;
		//获取栏目信息
		Channel indexChannel = null;
		List<Criterion> values = new ArrayList<Criterion>();
		values.add(Restrictions.eq("state", 1));
		Channel chan = this.channelDao.get(ElementConst.YQSBCONST);
		if ("".equals(index)) {
			indexChannel = chan;
		} else {
			indexChannel = this.channelDao.get(index);
			values.add(Restrictions.eq("channel.id", indexChannel.getId()));
		}
		//获取相对应信息.
		Pager deviceLst = this.findForPager(Device.class, new Order[]{Order.desc("level"), Order.desc("releaseDate")}, 
				startIndex, pageSize, values);
		List<FDeviceDTO> deviceDTO = new ArrayList<FDeviceDTO>();
		for (Device device : (List<Device>)deviceLst.getDatas()) {
			FDeviceDTO dto = new FDeviceDTO();
			BeanUtils.copyProperties(device, dto);
			dto.setUrl(ElementConst.YQSBCONST + "/" + device.getChannel().getNamespace() + "/" + device.getId() + ".jhtml");
			dto.setChannelName(device.getChannel().getName());
			deviceDTO.add(dto);
		}
		resMap.put("devices", deviceDTO);
		resMap.put("total", deviceLst.getTotal());
		resMap.put("pageIndex", page);
		int pages = 0;
		if (deviceLst.getTotal() !=0 && deviceLst.getTotal() % ElementConst.PAGENUM == 0) {
			pages = deviceLst.getTotal() / ElementConst.PAGENUM;
		} else {
			pages = deviceLst.getTotal() / ElementConst.PAGENUM + 1;
		}
		resMap.put("pages", pages);
		resMap.put("url", ElementConst.YQSBCONST + "/index.jhtml");
		resMap.put("title", chan.getName());
		//位置定位
		List<LocationDTO> locationDTO = new ArrayList<LocationDTO>();
		LocationDTO doo = new LocationDTO();
		doo.setName(chan.getName());
		doo.setUrl(ElementConst.YQSBCONST + "/index.jhtml");
		locationDTO.add(doo);
		if (!"".equals(index)) {
			LocationDTO doo1 = new LocationDTO();
			doo1.setName(indexChannel.getName());
			doo1.setUrl(ElementConst.YQSBCONST + "/" + index + "/index.jhtml");
			locationDTO.add(doo1);
			resMap.put("url", ElementConst.YQSBCONST + "/" + index + "/index.jhtml");
			resMap.put("title", indexChannel.getName());
		}
		resMap.put("locations", locationDTO);
		return resMap;
	}

	@Cacheable(value = "DEVICE_INFO_CACHE", key = "#id + #index + 'get'")
	public Map<String, Object> get(String index, Integer id) {
		//返回结果
		Map<String, Object> resMap = new HashMap<String, Object>();
		Device device = (Device)this.get(id);
		resMap.put("title", device.getTitle());
		resMap.put("txt", device.getTxt());
		//位置定位
		Channel channel = device.getChannel();
		Channel chan = this.channelDao.get(ElementConst.YQSBCONST);
		List<LocationDTO> locationDTO = new ArrayList<LocationDTO>();
		LocationDTO doo = new LocationDTO();
		doo.setName(chan.getName());
		doo.setUrl(ElementConst.YQSBCONST + "/index.jhtml");
		locationDTO.add(doo);
		LocationDTO doo1 = new LocationDTO();
		doo1.setName(channel.getName());
		doo1.setUrl(ElementConst.YQSBCONST + "/" + channel.getNamespace() + "/index.jhtml");
		locationDTO.add(doo1);
		resMap.put("locations", locationDTO);
		LocationDTO doo2 = new LocationDTO();
		doo2.setName("正文");
		doo2.setUrl(ElementConst.YQSBCONST + "/" + channel.getNamespace() + "/" + id + ".jhtml");
		locationDTO.add(doo2);
		return resMap;
	}

}
